package com.yunhe.sms.controller.activity;

import com.yunhe.commons.dto.Pageable;
import com.yunhe.commons.util.TimeUtils;
import com.yunhe.sms.entity.activity.Vote;
import com.yunhe.sms.service.activity.VoteItemService;
import com.yunhe.sms.service.activity.VoteService;
import com.yunhe.sms.service.activity.impl.VoteItemServiceImpl;
import com.yunhe.sms.service.activity.impl.VoteServiceImpl;
import com.yunhe.webmvc.annotation.RequestBean;
import com.yunhe.webmvc.annotation.RequestMapping;
import com.yunhe.webmvc.annotation.ResponseJson;
import com.yunhe.webmvc.model.JSONResponse;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by Administrator on 2017/10/30 0030.
 */
@RequestMapping("/vote")
public class VoteController {

    private VoteService voteService = new VoteServiceImpl();
    private VoteItemService voteItemService = new VoteItemServiceImpl();

    @RequestMapping
    public String list(){
        return "activity/vote/list";
    }

    @RequestMapping("/add")
    public String add(){
        return "activity/vote/add";
    }

    @RequestMapping("/edit")
    public String edit(Long id, HttpServletRequest req, String tabId){
        Vote vote = voteService.find(id);
        req.setAttribute("vote",vote);
        req.setAttribute("tabId",tabId);

        return "activity/vote/edit";


    }

    @RequestMapping("/save")
    @ResponseJson
    public JSONResponse save(@RequestBean Vote vote,String qzTime){
        if(null != qzTime){
            String[] times = qzTime.split("~");
            Date startTime = TimeUtils.StrToDateLong(times[0], TimeUtils.FORMAT_YYYY_MM_DD_HH_MM_SS);
            Date endTime = TimeUtils.StrToDateLong(times[1], TimeUtils.FORMAT_YYYY_MM_DD_HH_MM_SS);
            vote.setStartTime(startTime);
            vote.setEndTime(endTime);
        }
        Long id = voteService.save(vote);
        return JSONResponse.success("创建成功",id);
    }

    @RequestMapping("/delete")
    @ResponseJson
    public JSONResponse delete(String ids){
        voteService.deleteBatch(ids);
        return JSONResponse.success("删除成功");
    }

    @RequestMapping("/update")
    @ResponseJson
    public JSONResponse update(@RequestBean Vote vote,String qzTime){

        if(null != qzTime){
            String[] times = qzTime.split("~");
            Date startTime = TimeUtils.StrToDateLong(times[0], TimeUtils.FORMAT_YYYY_MM_DD_HH_MM_SS);
            Date endTime = TimeUtils.StrToDateLong(times[1], TimeUtils.FORMAT_YYYY_MM_DD_HH_MM_SS);
            vote.setStartTime(startTime);
            vote.setEndTime(endTime);
        }
        voteService.update(vote);
        return JSONResponse.success("修改成功");

    }
    @RequestMapping("/listData")
    @ResponseJson
    public Pageable<Vote> listDate(Integer offset, Integer limit){
        Pageable<Vote> votePageable = voteService.findPage(offset, limit);
        return votePageable;
    }


}
